/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.taiolib.indicators.movingAverage;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;
import java.util.List;

/**
 * 15/7/2010
 * @author Michael G. Langer
 */
public class SimpleMovingAverage implements MAStrategy {


    public List<BigDecimal> movingAverage(LinkedList<BigDecimal> inSerie, int value, int shift, MathContext mContext) {

        List<BigDecimal> result = new LinkedList<BigDecimal>();
        BigDecimal sum = new BigDecimal(0);
        BigDecimal divider = new BigDecimal(value, mContext);

        for (int i = 0; i <= inSerie.size(); i++) {
            if (i < value) {
                sum = sum.add(inSerie.get(i));
            } else if ( i == value ) {
                sum = sum.divide(divider, mContext);
                result.add(new BigDecimal(sum.doubleValue(), mContext));
            } else {
                sum = sum.subtract( inSerie.get(i-value-1).divide(divider, mContext) ).add( inSerie.get(i-1).divide(divider, mContext) );
                result.add(new BigDecimal(sum.doubleValue(), mContext));
            }
        }

        return result;
    }

    /**
     * This method returns the type of moving average, in this case a Simple Moving Average.
     * @return a MovingAverageType.SIMPLE
     */
    public MovingAverageType getType() {
        return MovingAverageType.SIMPLE;
    }

}
